import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nft_shop/box/conteroller/Invite_box_controller.dart';
import 'package:nft_shop/box/model/models.dart';
import 'package:nft_shop/res/constant.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/status_view.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class DialogMyChum extends StatefulWidget {
  late String ID;

  DialogMyChum({Key? key, required this.ID}) : super(key: key);

  @override
  State<DialogMyChum> createState() => _DialogMyChumState();
}

class _DialogMyChumState extends State<DialogMyChum> {
  InviteBoxController controller = InviteBoxController.to;

  ///刷新组件
  RefreshController refreshController =
  RefreshController(initialRefresh: false);
  int page = 1;
  ScrollController _scrollController = ScrollController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    controller.getInviteUserList(widget.ID, page, refreshController);
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        onLading(widget.ID);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    final Widget content = Material(
        color: Colors.transparent,
        child: Column(
          children: [
            Container(
              width: double.infinity,
              height: 330,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(5),
                  border: Border.all(color: Color(0xFFC6B395), width: 1),
                  color: Global.kTheme.textPrimary4),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Padding(
                    padding: const EdgeInsets.only(top: 17),
                    child: Text(
                      "我的好友",
                      style: TextStyle(
                        fontSize: 18,
                        color: Color(0xFFF2CAA6),
                      ),
                    ),
                  ),
                  GetBuilder<InviteBoxController>(
                      id: InviteBoxController.InviteBoxChum,
                      builder: (logic) {
                        if (logic.chumStatus != LoadStatusType.success) {
                          return Expanded(
                              child: StatusViews(
                                logic.chumStatus,
                                color: Colors.transparent,
                              ));
                        }
                        return Container(
                            height: 330 - 50,
                            padding: const EdgeInsets.only(top: 10, bottom: 5),
                            child: ListView.builder(
                                controller:_scrollController,
                                itemCount: logic.chumList.length,
                                physics: const AlwaysScrollableScrollPhysics(),
                                itemBuilder: (_, index) {
                                  return buildItem(logic.chumList[index]);
                                })

                          // SmartRefresher(
                          //     enablePullDown: true,
                          //     controller: refreshController,
                          //     header: WaterDropHeader(),
                          //     onRefresh: refresh(widget.ID),
                          //     onLoading: onLading(widget.ID),
                          //     child: ),
                        );
                      })
                ],
              ),
            ),
          ],
        ));

    final Widget body = MediaQuery.removeViewInsets(
      removeLeft: true,
      removeTop: true,
      removeRight: true,
      removeBottom: true,
      context: context,
      child: Center(
        child: InkWell(
          onTap: () {
            Get.back();
          },
          child: SizedBox(
            width: Get.width - 50,
            height: 380,
            child: content,
          ),
        ),
      ),
    );

    return AnimatedPadding(
      padding: MediaQuery
          .of(context)
          .viewInsets,
      duration: const Duration(milliseconds: 120),
      curve: Curves.easeInCubic, // easeOutQuad
      child: body,
    );
  }

  Widget buildItem(InviteUserList model) {
    return Container(
      height: 60,
      margin: EdgeInsets.symmetric(horizontal: 20),
      decoration: BoxDecoration(
          border: Border(
            bottom: Divider.createBorderSide(context,
                color: Color(0xFF2A2A2A), width: 1),
          )),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          ClipRRect(
              borderRadius: BorderRadius.circular(100),
              child: LoadImage(
                model.portrait ?? "",
                width: 41,
                height: 41,
              )),
          SizedBox(
            width: 6,
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  model.mobile ?? "",
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                      fontSize: 14, color: Global.kTheme.textPrimary5),
                ),
                SizedBox(
                  height: 2,
                ),
                Text(
                  model.createTime ?? "",
                  style: TextStyle(
                      fontSize: 12, color: Global.kTheme.textPrimary5),
                ),
              ],
            ),
          ),
          Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                '已完成',
                style:
                TextStyle(fontSize: 14, color: Global.kTheme.textPrimary5),
              ),
              Text(
                '${getUserState(model.real_name_status ?? 0)}',
                style:
                TextStyle(fontSize: 14, color: Global.kTheme.textPrimary5),
              )
            ],
          )
        ],
      ),
    );
  }

  String getUserState(int? realNameStatus) {
    String status = "未实名";
    if (realNameStatus == null && realNameStatus == "") {
      status = "0";
    }
    //是否完成身份认证 0未提交 1待审核 2审核通过 3驳回
    if (realNameStatus == 0) {
      status = "未实名";
    } else if (realNameStatus == 1) {
      status = "审核中";
    } else if (realNameStatus == 2) {
      status = "已实名";
    } else if (realNameStatus == 3) {
      status = "驳回";
    }

    return status;
  }

  refresh(id) {
    setState(() {
      page = 1;
    });
    controller.getInviteUserList(id, page, refreshController);
  }

  onLading(id) {
    setState(() {
      page += 1;
    });
    controller.getInviteUserList(id, page, refreshController);
  }
}
